<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
?>
<script>change_picture("admin");</script>
<div>
<h1>Manage Groups</h1>
<a href = "managehome.php">Back to Manage Home</a>
</div>
<div>
<form action = "#" method = "GET">
<label for = "name">Name: </label><input type = "text" name = "name" value = "<?php echo $_GET['name'];?>" />
<input type = "submit" value = "Search" />
<input type = "button" value = "Clear" onclick = "window.location.href = 'managegroups.php';" />
</form>
</div>
<div>
<a href="editgroup.php">Add a new group</a>
</div>
<hr />
<div>
<table>
<thead>
<td>ID</td><td>Name</td><td>Courses</td><td>Required Number</td><td>Operation</td>
</thead>
<tbody>
<?php
$filter = "true";
if (isset($_GET["index"]) && $_GET["index"] !== ""){
	$param = "(g_id = " . $mysqli->real_escape_string(trim($_GET["index"])) . ")";
	$filter .= " AND " . $param;
}
if (isset($_GET["name"]) && $_GET["name"] !== ""){
	$param = "(name LIKE '%" . $mysqli->real_escape_string(trim($_GET["name"])) . "%')";
	$filter .= " AND " . $param;
}
$perNumber = 25;
$page = $_GET['page'];
$count = $mysqli->query("select count(*) from Groups WHERE " . $filter . ";");
$rs = mysqli_fetch_array($count);
$totalNumber = $rs[0];
if ($totalNumber == 0){
	echo "<div><font color='red'>No result!</font></div>";
}
else{
	echo "<div>Found " . $totalNumber . " groups.</div>";
}
$totalPage = ceil($totalNumber / $perNumber);
if ($page < 1) {
	$page = 1;
}
else if ($page > $totalPage){
	$page = $totalPage;
}
$startCount = ($page - 1) * $perNumber;
$result = $mysqli->query("select * from Groups WHERE (" . $filter .") LIMIT $startCount, $perNumber");
if ($result){
	while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["g_id"];?></td>
<td><?php echo $row["name"];?></td>
<td><a href="javascript:" id = "toggle_course_link_<?php echo $row["g_id"];?>" onclick = "toggle_course_list(<?php echo $row["g_id"];?>);false;">Show</a><div id = "toggle_course_text_<?php echo $row["g_id"];?>"></div></td>
<td><?php echo $row["number"];?></td>
<td><a href="javascript:" onclick = "show_delete_yes(this,<?php echo $row['g_id'];?>);false;" >Delete</a>&nbsp&nbsp<a href="editgroup.php?id=<?php echo $row['g_id'];?>">Edit</a></td>
</tr>
<?php
	}
}
?>
</tbody>
</table>
<div>
<?php
if ($totalPage > 1){
	if ($page != 1) {
		echo "<a href='managegroups.php?name=" . $_GET["name"] . "&page=" . ($page - 1) . "'>Back</a>&nbsp";
	}
	for ( $i = 1; $i <= $totalPage; $i++){
		if ($i == $page){
			echo "&nbsp" . $i . "&nbsp";
		}
		else{
			echo "&nbsp<a href='managegroups.php?name=" . $_GET["name"] . "&page=" . $i . "'>" . $i . "</a>&nbsp";
		}
	}
	if ($page < $totalPage) {
		echo "&nbsp<a href='managegroups.php?name=" . $_GET["name"] . "&page=" . ($page + 1) . "'>Next</a>";
	}
}
?>
</div>
</div>
<script>
function toggle_course_list(id){
	if ($("#toggle_course_link_"+id).text() == "Show"){
		$("#toggle_course_link_"+id).text("Hide");
		$.post("findcourses_bygroup.php",
		{
			g_id:id,
		},
		function(data,status){
			$("#toggle_course_text_"+id).html(data);
		});
		$("#toggle_course_text_"+id).show();
	}
	else{
		$("#toggle_course_link_"+id).text("Show");
		$("#toggle_course_text_"+id).hide();
	}
}
function show_delete_yes(link, id){
	var top = $(link).offset().top;
	$("#deleteyes").css("display","block");
	$("#deleteyes").css("top", top);
	$("#g_id").val(id);
}
function hide_delete_yes(){
	$("#deleteyes").css("display","none");
}
</script>
<div id = "deleteyes" style="display:none;">
<div class = "confirm_delete_text">Do you really want to delete it?</div>
<form action = "deletegroup.php" method = "POST">
<input type = "hidden" name = "id" id = "g_id" />
<input type = "submit" value = "Yes" />
<input type = "button" value = "No" onclick = "hide_delete_yes();false;" />
</form>
</div>
<?php
}
include "footer.php";